/**
 * Created by liting on 2015/11/25.
 */
//验证上传文件框的文件类型  filetype
$.extend($.fn.validatebox.defaults.rules, {
    filetype: {
        validator: function(value, param){
            var name = value.split(".");
            //把类型转成小写的
            var stg = name[name.length-1].toLowerCase();
            var str = param[0].toLowerCase();
            return str.indexOf(stg)>-1;
        },
        message: '文件类型不对'
    }
});
//验证字符串字节数
$.extend($.fn.validatebox.defaults.rules, {
    lenReg: {
        validator: function(value, param){
            var cd = value.replace(/[^x00-xFF]/g,'**').length;
            return cd<param;
        },
        message: '内容太长'
    }
});
//身份证验证
$.extend($.fn.validatebox.defaults.rules, {
    missingMessage:'当文本框未填写时出现的提示信息',
    personnum: {
        validator: function(value){
            var telReg = !!value.match(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/);
            return telReg;
        },
        message: '身份证号格式不对'
    }
});
//用户密码验证
$.extend($.fn.validatebox.defaults.rules, {
    password: {
        validator: function(value){
            var telReg = !!value.match(/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,21}$/);
            return telReg;
        },
        message: '密码由8-21字母和数字组成，不能是纯数字或纯英文'
    }
});
//手机号码验证
$.extend($.fn.validatebox.defaults.rules, {
    phone: {
        validator: function(value){
            var telReg = !!value.match(/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/);
            return telReg;
        },
        message: '手机号格式不对'
    }
});
/**
 * Editor by wqy on 2015/11/26.
 */
$.extend($.fn.validatebox.defaults.rules, {
    equals: {
        validator: function(value,param){
            return value == $(param[0]).val();
        },
        message: '登录密码和确认密码不一致'
    },
    minLength: {
        validator: function(value, param){
            return value.length >= param[0];
        },
        message: '请至少输入{0}个字符'
    },
    filetypeCheck: {
        validator: function(value, param){
            var name = value.split(".");
            var bl_result = false;
            for(i=0;i<param.length;i++){
                if(name[1].indexOf(param[i])>-1){
                    bl_result = true;
                }
            }
            return bl_result;
        },
        message: '文件类型不对'
    },
    addressCheck: {
        validator: function(value, param){
            if(value==''||value=='请选择') return false;
            else return true;
        },
        message: '该输入项为必输项'
    },
    account: {// 验证帐号
        validator: function (value, param) {
            var bl_result = /^[a-zA-Z][a-zA-Z0-9_]{5,15}$/i.test(value);
            if(!bl_result){
                $.fn.validatebox.defaults.rules.account.message = '帐号不合法（字母开头，允许6-16字节，允许字母数字下划线）';
            }else{
                $.ajax({
                    url : param[0],
                    type : 'POST',
                    timeout : 60000,
                    data:{name:value},
                    async: false,
                    success : function(data) {
                        if(data=="false"){
                            bl_result = false;
                            $.fn.validatebox.defaults.rules.account.message = '账号已存在';
                        }else{
                            bl_result = true;
                        }
                    }
                });
            }
            return bl_result;
        },
        message: ''
    },
    dxyzm: {// 短信验证码
        validator: function (value, param) {
            return /^[0-9_]{4}$/i.test(value);
        },
        message: '验证码不合法（只允许是4位数字）'
    }
});
$.extend($.fn.validatebox.methods, {
    remove: function(jq, newposition){
        return jq.each(function(){
            $(this).removeClass("validatebox-text validatebox-invalid").unbind('focus.validatebox').unbind('blur.validatebox');
        });
    },
    reduce: function(jq, newposition){
        return jq.each(function(){
            var opt = $(this).data().validatebox.options;
            $(this).addClass("validatebox-text").validatebox(opt);
        });
    }
});
/***下拉框必选 默认选择 不做数，
 * 默认请选择  value为$select
 * data-options="validType:'selected'
 * <option value="$select">请选择许可类型</option>
 * ***/
$.extend($.fn.validatebox.defaults.rules, {
    selectcheck: {
        validator: function(value){
            return value!="$select"&&value!=null&&value!=""&&value!="请选择许可类型";
        },
        message: '类型必须选择！'
    }
});

$.extend($.fn.validatebox.defaults.rules, {
    codecheck: {
        validator: function(value,parm){
            var result = false;
            if(value) result = value.toUpperCase()==parm.toUpperCase();
            return result;
        },
        message: '验证码错误'
    }
});
$.extend($.fn.validatebox.defaults.rules, {
    youbian: {// 验证邮政编码
        validator: function (value) {
            return /^[1-9]\d{5}$/i.test(value);
        },
        message: '邮政编码格式不正确'
    }
});
var xsscharSet = ["select","*","<script", "</script>",  "document.", "eval(", "web.xml", "%0d", "%0a", "alert(", "\r", "\n", "javascript:", "prompt(", "confirm(", "<iframe", "<frameset", "</iframe>", "</frameset>", "<frame", "</frame", "onabort=", "onblur=", "onchange=", "onclick=", "ondblclick=", "onerror=", "onfocus=", "onkeydown=", "onkeypress=", "onkeyup=", "onload=", "onmousedown=", "onmousemove=", "onmouseout=", "onmouseover=", "onmouseup=", "onreset=", "onresize=", "onselect=", "onsubmit=", "onunload="]
$.extend($.fn.validatebox.defaults.rules, {
    xss: {
        validator: function(value){
            var rs = true;
            for(var i = 0;i<xsscharSet.length;i++) {
                //alert(value.toLowerCase());
                if (value.toLowerCase().indexOf(xsscharSet[i]) > -1) {
                    rs = false;
                    break;
                }
            }
            return rs;
        },
        message: '非法字符！'
    }
});
